package com.zyj.guava.multimap;

import com.google.common.collect.ListMultimap;
import com.google.common.collect.MultimapBuilder;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.List;

public class MultimapSample {

    @Data
    @AllArgsConstructor
    private static class Answer {
        private Integer num;//题号
        private String name;//姓名
        private Integer score;//得分
    }

    private static List<Answer> answerList = List.of(
            new Answer(1, "小明", 10),
            new Answer(1, "铁柱", 2),
            new Answer(2, "小明", 8),
            new Answer(2, "铁柱", 6),
            new Answer(3, "小明", 1),
            new Answer(3, "铁柱", 1),
            new Answer(4, "铁柱", 5),
            new Answer(5, "铁柱", 20),
            new Answer(6, "铁柱", 3)
    );

    public static void main(String[] args) {
        ListMultimap<String, Integer> answerMultimap = MultimapBuilder.hashKeys().arrayListValues().build();
        for (Answer answer : answerList) {
            answerMultimap.put(answer.getName(), answer.getScore());
        }
        System.out.println(answerMultimap);
        for (String name : answerMultimap.keySet()) {
            System.out.println("name : " + name);
            List<Integer> scores = answerMultimap.get(name);
            int sumScore = scores.stream().mapToInt(Integer::intValue).sum();
            System.out.println("总分:" + sumScore);
            System.out.println("-----------------");
        }
    }
}
